<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class AppModel extends Model {
    
    protected $_tablename = '';
    protected $_primary_key = 'id';
    public $admin_id = '';
    protected $_loaded = FALSE;
    protected $_numrow = 1;

    /**
     * Constructor
     *
     * @access public
     */
    function AppModel($id = NULL) {
        parent::Model();
        if (empty($this->_tablename))
            $this->_tablename = strtolower(get_class($this));
        /*
        $CI = & get_instance();		
        $fid = $CI->input->get('fid');		
        if(!empty($fid) && is_numeric($fid)){
            $this->admin_id = (int)$fid;
        }else{
            $CI->load->module_library('users.auth');
            $auth_data = $CI->auth->get_auth_data();
            $this->admin_id = $auth_data['admin_id'];
        }
        */
        if (!is_null($id)) {
            $this->set_id($id);
            $this->get_by_fid();
        }
        log_message('debug', "AppModel Class Initialized");
    }
    
    function get() {
        if (empty($this->{$this->_primary_key})) {
            $this->_loaded = FALSE;
            return $this->is_loaded();
        }

        $this->db->where($this->_primary_key, $this->{$this->_primary_key});
        $res = $this->db->get($this->_tablename);
        if ($res->num_rows() > 0) {
            $row = $res->row_array();
            $this->_field_mapping($row);
            $this->_loaded = TRUE;
        } else {
            $this->_loaded = FALSE;
        }
        return $this->is_loaded();
    }
    
    function get_by_fid() {
        if (empty($this->{$this->_primary_key})) {
            $this->_loaded = FALSE;
            return $this->is_loaded();
        }

        $this->db->where($this->_primary_key, $this->{$this->_primary_key});
        //$this->db->where('admin_id', $this->admin_id);
        $res = $this->db->get($this->_tablename);

        if ($res->num_rows() > 0) {
            $row = $res->row_array();
            $this->_field_mapping($row);
            $this->_loaded = TRUE;
        } else {
            $this->_loaded = FALSE;
        }
        return $this->is_loaded();
    }

    function custom_get($condition_array = array()) {
        if (empty($this->{$this->_primary_key})) {
            $this->_loaded = FALSE;
            return $this->is_loaded();
        }

        $this->db->where($this->_primary_key, $this->{$this->_primary_key});
        $this->db->where('admin_id', $this->admin_id);
        foreach ($condition_array as $k=>$v) {
            $this->db->where($k, $v);
        }
        $res = $this->db->get($this->_tablename);

        if ($res->num_rows() > 0) {
            $row = $res->row_array();
            $this->_field_mapping($row);
            $this->_loaded = TRUE;
        } else {
            $this->_loaded = FALSE;
        }
        return $this->is_loaded();
    }

    function save_by_fid($data) {
        $this->db->set($data);
        if ($this->get_by_fid()) {
            $this->db->where($this->_primary_key, $this->{$this->_primary_key});
            $this->db->where('admin_id', $this->admin_id);
            return $this->db->update($this->_tablename);
        } else {
            return $this->db->insert($this->_tablename);
        }
    }

    function delete_by_fid() {
        $this->db->where($this->_primary_key, $this->{$this->_primary_key});
        $this->db->where('admin_id', $this->admin_id);
        $this->db->delete($this->_tablename);
    }
    
    function get_id() {
        return @$this->{$this->_primary_key};
    }
    
    function set_id($id) {
        $this->{$this->_primary_key} = (int) $id;
    }
    
    function delete() {
        $this->db->where($this->_primary_key, $this->{$this->_primary_key});
        $this->db->delete($this->_tablename);
    }
    
    function get_lists($conditions = NULL, $limit = NULL, $offset = NULL, $order_by = NULL, $order_type = 0) {
        if (!empty($conditions))
            $this->db->where($conditions);
        $this->_numrow = $this->db->count_all_results($this->_tablename);
        if ($this->_numrow > 0) {
            if (!empty($conditions))
                $this->db->where($conditions);
            $this->db->limit($limit);
            $this->db->offset($offset);
            if (!is_null($order_by)) {
                $s = $this->sort_field($order_by) . $this->sort_type($order_type);
                $this->db->order_by($s);
            }
            $query = $this->db->get($this->_tablename);
            if (empty($this->_mapping)) {
                return $query->result();
            } else {
                $res = array();
                foreach ($query->result() as $r) {
                    foreach ($this->_mapping as $map)
                        $r->{key($map)} = $r->{current($map)};
                    $res[] = $r;
                }
                return $res;
            }
        }
        return array();
    }
    
    function is_loaded() {
        return $this->_loaded;
    }
    
    function get_numrows() {
        return $this->_numrow;
    }
    
    function save($data) {
        $this->db->set($data);
        if ($this->get_id()) {
            $this->db->where($this->_primary_key, $this->{$this->_primary_key});
            return $this->db->update($this->_tablename);
        } else {
            return $this->db->insert($this->_tablename);
        }
    }
    
    public function sort_field($idx = NULL) {
        if (is_null($idx))
            return $this->_primary_key;

        if (empty($this->_mapping))
            return $idx;

        $idx = strtolower($idx);
        foreach ($this->_mapping as $m)
            if (key($m) == $idx)
                return current($m);

        return $this->_primary_key;
    }
    
    public function sort_type($idx = 0) {
        return $idx == 1?' DESC ':' ASC ';
    }
    
    function _field_mapping($row) {
        if (empty($this->_mapping)) {
            foreach ($row as $k=>$v)
                $this->{$k} = $v;
        } else {
            foreach ($this->_mapping as $map) {
                if (current($map) == $this->_primary_key)
                    $this->set_id($row[current($map)]);
                $this->{key($map)} = $row[current($map)];
            }
        }
    }
}
